package com.hsoft.zhxy.app.demo_03_01.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsoft.zhxy.app.demo_03_01.mapper.DemoMapper;
import com.hsoft.zhxy.app.demo_03_01.model.entity.DemoEntity;
import com.hsoft.zhxy.app.demo_03_01.model.query.DemoQuery;
import com.hsoft.zhxy.app.demo_03_01.model.vo.DemoVO;
import com.hsoft.zhxy.app.demo_03_01.service.DemoService;
import com.hsoft.zhxy.common.base.BaseService;
import com.hsoft.zhxy.common.result.ResultBody;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * All rights Reserved, Designed By 哈尔滨工业大学软件工程股份有限公司
 *
 * @Title: DEMO通用service
 * @Package
 * @Description:
 * @author: zhouzhuoqun
 * @date: 2020-10-24
 */
@Log4j2
@Service
public class DemoServiceImpl extends BaseService implements DemoService {

    @Autowired
    DemoMapper demoMapper;

    /**
     * 查询单条数据
     * @param demoEntity
     * @return JSONObject
     */
    @Override
    public JSONObject findOne(DemoEntity demoEntity) {
        DemoEntity result = demoMapper.selectOne(new QueryWrapper<DemoEntity>().setEntity(demoEntity));
        return ResultBody.setData(result);
    }

    /**
     * 查询列表数据
     * @param demoEntity
     * @return JSONObject
     */
    @Override
    public JSONObject findList(DemoEntity demoEntity) {
        List<DemoEntity> result = demoMapper.selectList(new QueryWrapper<DemoEntity>().setEntity(demoEntity));
        return ResultBody.setData(result);
    }

    /**
     * 查询分页数据
     * @param demoEntity
     * @param page
     * @param pageSize
     * @return JSONObject
     */
    @Override
    public JSONObject findListByPage(DemoEntity demoEntity, long page, long pageSize) {
        Page<DemoEntity> pageObj = new Page<DemoEntity>(page, pageSize);
        IPage<DemoEntity> result = demoMapper.selectPage(pageObj, new QueryWrapper<DemoEntity>().setEntity(demoEntity));

        return ResultBody.setData(result.getTotal(), result.getRecords());
    }

    /**
     * 查询用户基础信息数据(分页)
     * @param demoQuery
     * @return JSONObject
     */
    @Override
    public JSONObject findListYhjcxxByPage(DemoQuery demoQuery) {
        //新建分页对象
        Page<DemoQuery> pageObj = new Page<DemoQuery>(demoQuery.getPage(), demoQuery.getPageSize());
        //新建条件构造器
        QueryWrapper<DemoQuery> queryWrapper = new QueryWrapper<>();
        //最后拼接后的sql为  where yh.yhid like '%${yhid}%' and yh.xm like '%${xm}%' and yh.yhzt = #{yhzt} and bm.bmm = #{bmm} order by yh.cjsj
        queryWrapper.like(!StringUtils.isBlank(demoQuery.getYhid()),"yh.yhid", demoQuery.getYhid())
                .like(!StringUtils.isBlank(demoQuery.getXm()),"yh.xm",demoQuery.getXm())
                .eq(demoQuery.getYhzt() != null,"yh.yhzt" , demoQuery.getYhzt())
                .eq(!StringUtils.isBlank(demoQuery.getBmm()),"bm.bmm",  demoQuery.getBmm())
                .orderByAsc("yh.cjsj");

        IPage<DemoVO> result = demoMapper.findListYhjcxxByPage(pageObj, queryWrapper);
        return ResultBody.setData(result.getTotal(), result.getRecords());
    }

    /**
     * 删除
     * @param demoEntity
     * @return JSONObject
     */
    @Override
    public JSONObject delete(DemoEntity demoEntity) {
        return ResultBody.setData(demoMapper.delete(new QueryWrapper<DemoEntity>().setEntity(demoEntity)));
    }

    /**
     * 新增
     * @param demoEntity
     * @return JSONObject
     */
    @Override
    public JSONObject add(DemoEntity demoEntity) {
        return ResultBody.setData(demoMapper.insert(demoEntity));
    }

    /**
     * 修改
     * @param demoEntity
     * @return JSONObject
     */
    @Override
    public JSONObject update(DemoEntity demoEntity) {
        return ResultBody.setData(demoMapper.updateById(demoEntity));
    }

}
